var deviceone = require("deviceone");
var nf = deviceone.sm("do_Notification");
var http = require("helper/http");
var open = require("helper/open");
var payUrl = require("url/pay");
var orderUrl = require("url/order");
var mallUrl = require("url/mall");

module.exports.type = {
	/** 服务 * */
	HOMEMAKING : 1,
	/** 圈圈商城 * */
	QUANMALL : 2,
	/**闲置品* */
	IDLE : 3,
	/**活动* */
	ACTIVITY : 4,
	/**圈商* */
	QSHOP : 6
};
/**
 * 买家支付订单
 */
module.exports.toPay = function(orderNo){
	if(!orderNo || orderNo==null || orderNo=="undefined"){
		nf.toast("订单提交失败");
		return;
	}
	http.post(payUrl.pay.checkForPay,{"orderNo":orderNo},function(data){
		open.open("source://view/order/pay/main.ui",data);
	});
};
/**
 * 买家取消订单
 */
module.exports.buyerCancel = function(orderNo,callback){
	if(!orderNo || orderNo==null || orderNo=="undefined"){
		nf.toast("订单取消失败");
		return;
	}
	http.post(orderUrl.url.handle.buyerCancel,{"orderNo":orderNo},function(data){
		if(callback && typeof callback == "function") {
			callback();
	    }else{
			nf.toast("订单已取消");
		}
	});
};

/**
 * 商城订单取消
 */
module.exports.cancelMallOrder = function(orderNo,callback){
	if(!orderNo || orderNo==null || orderNo=="undefined"){
		nf.toast("订单取消失败");
		return;
	}
	http.post(mallUrl.url.order.doCancel,{"orderNo":orderNo},function(data){
		if(callback && typeof callback == "function") {
			callback();
	    }else{
			nf.toast("订单已取消");
		}
	});
};
/**
 * 卖家取消订单(服务)
 */
module.exports.sellerCancel = function(orderNo,callback){
	if(!orderNo || orderNo==null || orderNo=="undefined"){
		nf.toast("订单取消失败");
		return;
	}
	http.post(orderUrl.url.handle.sellerCancel,{"orderNo":orderNo},function(data){
		if(callback && typeof callback == "function") {
			callback();
	    }else{
			nf.toast("订单已取消");
		}
	});
};
/**
 * 买家确认收货，签收
 */
module.exports.buyerSign = function(orderNo,callback){
	if(!orderNo || orderNo==null || orderNo=="undefined"){
		nf.toast("订单确认失败");
		return;
	}
	http.post(orderUrl.url.handle.signed,{"orderNo":orderNo},function(data){
		if(callback && typeof callback == "function") {
			callback();
	    }else{
			nf.toast("已签收");
		}
	});
};
/**
 * 买家申请退货(未签收的情况下)
 * orderDelivery:退货Json数据，对照后台的OrderDelivery类
 */
module.exports.buyerApplyReturn = function(orderDelivery,callback){
	if(!orderDelivery || orderDelivery==null || orderDelivery=="undefined"){
		nf.toast("申请退货数据异常");
		return;
	}
	http.post(orderUrl.url.handle.applyReturn,orderDelivery,function(data){
		if(callback && typeof callback == "function") {
			callback();
	    }else{
			nf.toast("已申请退货，等待卖家确认");
		}
	});
};
/**
 * 卖家发货
 * orderNo:订单号
 * hasNeedDelivery:是否需要物流：'1'需要，'2'不需要
 * deliveryName:快递或物流公司名称(家政订单可以为空)
 * deliveryNo:快递或物流单号(家政订单可以为空)
 */
module.exports.sellerSended = function(orderNo,hasNeedDelivery,deliveryName,deliveryNo,sendDeliveryType,callback){
	if(!orderNo || orderNo==null || orderNo=="undefined"){
		nf.toast("订单号异常");
		return;
	}
	var params = {
		'orderNo' : orderNo,
		'hasNeedDelivery' : hasNeedDelivery,
		'deliveryName' : deliveryName,
		'deliveryNo' : deliveryNo,
		'sendDeliveryType':sendDeliveryType
	};
	http.post(orderUrl.url.handle.sended,params,function(data){
		if(callback && typeof callback == "function") {
			callback();
	    }else{
			nf.toast("成功发货");
		}
	});
};

/**
 * 买家申请退款
 * orderNo:订单号
 */
module.exports.applyReturnPay = function(orderNo,callback){
	if(!orderNo || orderNo==null || orderNo=="undefined"){
		nf.toast("申请退款数据异常");
		return;
	}
	http.post(orderUrl.url.handle.applyReturnPay,{'orderNo':orderNo},function(data){
		if(callback && typeof callback == "function") {
			callback();
	    }else{
			nf.toast("已申请退款，等待卖家审核");
		}
	});
};

/**
 * 卖家同意退款
 * orderNo:订单号
 */
module.exports.sellerAgreeReturnPay = function(orderNo,callback){
	if(!orderNo || orderNo==null || orderNo=="undefined"){
		nf.toast("审核数据异常");
		return;
	}
	http.post(orderUrl.url.handle.sellerAgreeReturnPay,{'orderNo':orderNo},function(data){
		if(callback && typeof callback == "function") {
			callback();
	    }else{
			nf.toast("退款完成");
		}
	});
};

/**
 * 卖家拒绝退款
 * orderNo:订单号
 */
module.exports.sellerRefuseReturnPay = function(orderNo,callback){
	if(!orderNo || orderNo==null || orderNo=="undefined"){
		nf.toast("审核数据异常");
		return;
	}
	http.post(orderUrl.url.handle.sellerRefuseReturnPay,{'orderNo':orderNo},function(data){
		if(callback && typeof callback == "function") {
			callback();
	    }else{
			nf.toast("已拒绝退款");
		}
	});
};